<?php



/**
 * 充值管理
 * Enter description here ...
 * @author Admin
 *
 */
class PaymentAction extends CommonAction{



	public function alipay_notify_url()    {
		$data = Init_GP(array('out_trade_no','total_fee')); //out_trade_no定单号 total_fee总价
		import("@.ORG.Alipay");
		$alipay = new Alipay();
		$info = $alipay->_notify_url();
		if ($info){
			//成功执行需要操作的方法
			$this->succ($data['out_trade_no'],$data['total_fee']);
		}else{
			//失败方法
		}
	}


	public function alipay_return_url()    {
		$data = Init_GP(array('out_trade_no','total_fee'));
		import("@.ORG.Alipay");
		$alipay = new Alipay();
		$info = $alipay->_return_url();
		if ($info === true){
			//成功执行需要操作的方法
			$this->succ($data['out_trade_no'],$data['total_fee']);
			$json = '支付成功';
		}elseif ($info === false){
			//失败
			$json = '支付失败';
		}else{
			$json = $info; //返回状态码
		}
		$this->assign('json',$json);
		$this->display();
	}

	public function yeepay_return_url()	{
		$data = Init_GP(array('r6_Order','r3_Amt'));
		import("@.ORG.Yeepay");
		$yeepay = new Yeepay();
		$info = $yeepay->_return_url();
		if ($info === true){
			//成功执行需要操作的方法
			$this->succ($data['r6_Order'],$data['r3_Amt']);
			$json = '支付成功';
		}elseif ($info === false){
			//失败
			$json = '支付失败';
		}else{
			$json = $info; //返回状态码
		}
		$this->assign('json',$json);
		$this->display();
	}

	public function chinabank_return_url()	{
		$data = Init_GP(array('v_oid','v_amount'));
		import("@.ORG.Chinabank");
		$chinabank = new Chinabank();
		$info = $chinabank->_return_url();
		if ($info === true){
			//成功执行需要操作的方法
			$this->succ($data['v_oid'],$data['v_amount']);
			$json = '支付成功';
		}elseif ($info === false){
			//失败
			$json = '支付失败';
		}else{
			$json = $info; //返回状态码
		}
		$this->assign('json',$json);
		$this->display();
	}

	public function tenpay_return_url()	{
		$data = Init_GP(array('sp_billno','total_fee'));
		import("@.ORG.Tenpay");
		$tenpay = new Tenpay();
		$info = $tenpay->_return_url();
		if ($info === true){
			//成功执行需要操作的方法
			$this->succ($data['sp_billno'],$data['total_fee'] / 100);
			$json = '支付成功';
		}elseif ($info === false){
			//失败
			$json = '支付失败';
		}else{
			$json = $info; //返回状态码
		}
		$this->assign('json',$json);
		$this->display();
	}

	
	
	
	
	/**
	 * 支付成功
	 * Enter description here ...
	 * @param unknown_type $sn
	 * @param unknown_type $amount
	 * @param unknown_type $type
	 */
	public function succ($sn,$amount,$type){
		$pre = substr($sn,0,2);
		if ($pre == 'RE'){
			//查找哪条充值订单
			$paymentorder = D('Paymentorder');
			$map = array(
    			'sn'=>array('eq',$sn),
    			'state'=>array('eq',0)
			);
			$data = $paymentorder->where($map)->find();
			if (!empty($data) && $data['amount'] == $amount){
				//修改订单状态
				$paymentordermap['id']=array('eq',$data['id']);
				$paymentorderdata = array(
    				'state'=>1,
    				'successtime'=>time()
				);
				$paymentorder->where($paymentordermap)->save($paymentorderdata);
				//修改用户信息
				$pid = $data['pid'];
				$user = D('User');
				$config = C('SYSCONFIG');
				
				if (empty($pid)){
					$beat = $data['amount'] * $config['rmb_beat'];     //实体币 = 购买数量 *商品比和实体货币折算金额
					$user->_setbeat($data['uid'],$beat);
					$remark = "用户{$data['payment']}充值{$beat}{$config['beat_name']}";
				}else {
					//....根据竞标币套餐获取实体币和增返还虚拟币信息
					$package = D('Package');
					$packagedata = $package->find($pid);
					$beat = $packagedata['beat'];
					$pointb = $packagedata['pointb'];
					
					//..用户相关实体币虚拟币递增相应额度
					$user->setInc('beat',"id = {$data['uid']}",$beat);
					$user->setInc('pointb',"id = {$data['uid']}",$pointb);
					$remark = "用户{$data['payment']}充值{$beat}{$config['beat_name']},赠送{$pointb}{$config['pointb_name']}";
				}
				
				
				//添加交易日志
				$transaction = D('Transaction');
				$transactiondata = array(
					'uid'=>$data['uid'],
					'name'=>$data['name'],
					'payment'=>'充值',
    				'money'=>$data['amount'],
					'remark'=>$remark,
				
					'type'=>"beat",  //虚拟币
					'source'=>'8',
					'robot'=>'0'
				
				);
				$transaction->insert($transactiondata);
				
				
			}
		}elseif ($pre == 'PA'){
			$order = D('Order');
			$map = array(
    			'ordersn'=>array('eq',$sn),
    			'state'=>array('eq',0)
			);
			$data = $order->where($map)->find();

			if (!empty($data) && $data['orderprice'] == $amount){
				//修改订单状态
				$ordermap['id']=array('eq',$data['id']);
				$orderdata = array(
    				'state'=>1
				);
				$order->where($ordermap)->save($orderdata);
				
				
				//添加交易日志
				$transaction = D('Transaction');
				$transactiondata = array(
						'uid'=>$data['uid'],
						'name'=>$data['name'],
						'payment'=>'支付订单',
    					'money'=>$data['orderprice'],
						'remark'=>"用户支付{$data['goodsname']}定单号{$data['ordersn']}" ,
						
						'type'=>"goods",  //商品订单
						'source'=>'9',
						'robot'=>'0'
				
				);
				$transaction->insert($transactiondata);
				
				
			}
		}
	}
	
	
	
	
	
}
?>